##### Install packages #####

packages <- c("ggrepel", "cdlTools", "tidyverse", "GGally", "reshape", "ggeffects", "stringr", "dplyr", "RColorBrewer", "cobalt", "backports","ggplot2", "ggridges","scales", "gridExtra", "egg", "ggpubr", "grid", "reshape2", "haven")

packages_installed <-
  sapply(packages, function(pack)
    pack %in% rownames(installed.packages()))
if (any(!packages_installed)) {
  sapply(packages[!packages_installed], install.packages)
}
sapply(packages, require, character.only = TRUE)
rm(packages,packages_installed)

##### Read Data #####

load("state.RData")
load("state_year.RData")
cces <- read_dta("KimPelc_vaccine_main.dta")

# Figure 1. Distribution of Ideological Distance Measure

cces <- within(cces, {   
  ideo_cat <- NA 
  ideo_cat[ideo7 > 4] <- "Conservative"
  ideo_cat[ideo7 == 4] <- "Moderate"
  ideo_cat[ideo7 < 4] <- "Liberal"
  
  ideo_cat7 <- NA 
  ideo_cat7[ideo7==7] <- "Very conservative"
  ideo_cat7[ideo7==6] <- "Conservative"
  ideo_cat7[ideo7==5] <- "Somewhat conservative"
  ideo_cat7[ideo7==4] <- "Moderate"
  ideo_cat7[ideo7==3] <- "Somewhat liberal"
  ideo_cat7[ideo7==2] <- "Liberal"
  ideo_cat7[ideo7==1] <- "Very liberal"
  
} )

cces$ideo_cat <- ordered(cces$ideo_cat, levels = c("Conservative", "Moderate", "Liberal"))
cces$ideo_cat7 <- ordered(cces$ideo_cat7, levels = c("Very conservative", "Conservative", "Somewhat conservative", "Moderate", "Somewhat liberal", "Liberal", "Very liberal"))


pdf("Figure1A.pdf",width=6,height=6)
ggplot(cces[which(is.na(cces$ideo_cat)!=TRUE),], 
       aes(x = st_ideo7_dist2021, 
           y = ideo_cat7)) +  
  stat_density_ridges(quantile_lines = TRUE, quantiles = 2, inherit.aes = TRUE) + 
  theme_ridges() + theme(legend.position = "none", 
                         axis.title.x = element_text(color="black", size=13, face="bold"),
                         axis.text.y = element_text(color= "black", size=13, face="bold"),
                         axis.text.x = element_text(color= "black", size=13, face="bold"),
                         axis.title.y = element_text(color="black", size=13, face="bold"))  + 
  geom_density_ridges(
    jittered_points = TRUE, 
    position = position_points_jitter(width = 0.05, height = 0),
    point_shape = "|", point_size = 2,  point_alpha = 0.8, alpha = 0.7,
    aes(fill= ideo_cat7)) +  
  xlab("Ideological Distance to State Average") + ylab("") +
  scale_fill_manual(values = rev(c("#002F70", "#517AC9", "#B4C2EB", "#F6F6F6", "#EDB4B5", "#C05D5D", "#5F1415")))
dev.off()

pdf("Figure1B.pdf",width=6,height=6)
ggplot(cces[which(is.na(cces$ideo_cat)!=TRUE),], 
       aes(x = cz_ideo7_dist2021, 
           y = ideo_cat7)) +  
  stat_density_ridges(quantile_lines = TRUE, quantiles = 2, inherit.aes = TRUE) + 
  theme_ridges() + theme(legend.position = "none", 
                         axis.title.x = element_text(color="black", size=13, face="bold"),
                         axis.text.y = element_text(color= "black", size=13, face="bold"),
                         axis.text.x = element_text(color= "black", size=13, face="bold"),
                         axis.title.y = element_text(color="black", size=13, face="bold"))  + 
  geom_density_ridges(
    jittered_points = TRUE, 
    position = position_points_jitter(width = 0.05, height = 0),
    point_shape = "|", point_size = 2,  point_alpha = 0.8, alpha = 0.7,
    aes(fill= ideo_cat7)) +  
  xlab("Ideological Distance to Commuting Zone Average") + ylab("") +
  scale_fill_manual(values = rev(c("#002F70", "#517AC9", "#B4C2EB", "#F6F6F6", "#EDB4B5", "#C05D5D", "#5F1415")))
dev.off()

# Figure 2. Average Ideological Distance by State Over Time. 

pdf("Figure2.pdf",width=7,height=5)
ggplot(average_distance, aes(x=year, y=avg_abs_distance, group=state_fips, color = group)) + geom_line(size=0.5) +
  scale_color_manual(values = c("black", "gray") ) + theme_minimal() + labs(y="Average Ideological Distance within State", x = "Year") +
  theme(legend.position="none")
dev.off()

# Figure 3. Vaccination Rates by Ideology and Ideological Polarization

cces1 <- ggplot(data = state, mapping = aes(st_ideo7_mean2021, cdc_first_prop)) + 
  geom_point() + 
  geom_smooth(method="lm") + xlab("Mean of Ideology") + ylab("First Dose (%)") + ggtitle("CES") +
  theme_classic()

cces2 <- ggplot(data = state, mapping = aes(st_ideo7_dist2021, cdc_first_prop)) + 
  geom_point() + 
  geom_smooth(method="lm") + xlab("Average Distance of Ideology") + ylab("First Dose (%)") + ggtitle("CES") + 
  theme_classic()

pdf("Figure3.pdf",width=7,height=3.5)
grid.arrange(cces1, cces2, ncol=2)
dev.off()



# Figure A1: The Comparison of CES Survey and CDC Vaccination Rate across States

pdf("FigureA1.pdf",width=5,height=5)
ggplot(state, aes(x=cdc_first_prop, y=cces_vaccine_prop_wt, label=Recip_State)) + 
  scale_x_continuous(name="CDC: First Dose (%)", limits=c(48,93)) +
  scale_y_continuous(name="CES: Vaccinated (%)", limits=c(48,93)) +
  geom_point() + geom_abline(slope = 1) + geom_text_repel(size=3)
dev.off()

# Figure A2: The Comparison of Regional Affective Polarization and Political Diversity

pdf("FigureA2.pdf",width=5,height=5)

ggplot(data = state, mapping = aes(st_ideo7_var, affective_polarization_avg)) + 
  geom_point() + ylim(0,100) +
  geom_smooth(method="lm") + xlab("Variance of Ideology") + ylab("Affective Polarization")  

dev.off()
